package com.huawei.statistics.utils;


import com.huawei.statistics.pojo.report.Area;
import com.huawei.statistics.pojo.report.City;
import com.huawei.statistics.pojo.report.Province;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Created by Yinwx on 2019/7/19.
 */
public class XMLUtil {

    public static List<Province> proList = new ArrayList<>();
    public static List<City> cityList  = new ArrayList<>();
    public static List<Area> areaList  = new ArrayList<>();


    public static void getProList(){
        long lasting = System.currentTimeMillis();
        try {
            //String path = PathKit.class.getResource("/123.txt").getPath() ;
            File f = ResourceUtils.getFile("classpath:data/Provinces.xml");
            System.out.println(f.getName());
            SAXReader reader = new SAXReader();
            Document doc = reader.read(f);
            Element root = doc.getRootElement();
            Element element;
            for (Iterator i = root.elementIterator("Province"); i.hasNext(); ) {
                element = (Element) i.next();
                Province province = new Province();
                String pid = element.attributeValue("ID");
                province.setId(pid);
                province.setName(element.attributeValue("ProvinceName"));
                proList.add(province);
            }
            //System.out.println(proList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getCity(){
        try {
            File f = ResourceUtils.getFile("classpath:data/Cities.xml");
            SAXReader reader = new SAXReader();
            Document doc = reader.read(f);
            Element root = doc.getRootElement();
            Element element;
            for (Iterator i = root.elementIterator("City"); i.hasNext(); ) {
                element = (Element) i.next();
                City city = new City();
                String cid = element.attributeValue("ID");
                city.setId(cid);
                city.setName(element.attributeValue("CityName"));
                city.setProvinceId(element.attributeValue("PID"));
                cityList.add(city);
            }
            //System.out.println(cityList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static List<Province> setDate(){
        long start = System.currentTimeMillis();
        getProList();
        getCity();
        getArea();
        List<Province> resultList = new ArrayList();
        for(Province province : proList){
            List<City> citylist = new ArrayList<>();
            for(City city : cityList){
                if(province.getId().equals(city.getProvinceId())){
                    List<Area> areas = new ArrayList<>();;
                    for(Area area : areaList){
                        if(city.getId().equals(area.getCityId())){
                            areas.add(area);
                        }
                    }
                    city.setAreaList(areas);
                    citylist.add(city);
                }
            }
            province.setCityList(citylist);
            resultList.add(province);

        }
        System.out.println(resultList);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        return resultList;

    }

    public static void getArea(){

        try {
            File f = ResourceUtils.getFile("classpath:data/Districts.xml");
            SAXReader reader = new SAXReader();
            Document doc = reader.read(f);
            Element root = doc.getRootElement();
            Element element;
            for (Iterator i = root.elementIterator("District"); i.hasNext(); ) {
                element = (Element) i.next();
                Area area = new Area();
                String aid = element.attributeValue("ID");
                area.setId(aid);
                area.setAreaName(element.attributeValue("DistrictName"));
                area.setCityId(element.attributeValue("CID"));
                areaList.add(area);
            }
            System.out.println(areaList);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }




    public static void main(String[] args) {









    }

}
